<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
       <el-table
        :data="list"
        style="width: 100%"
        border>
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            prop="cate_name"
            label="分类名称"
            >
          </el-table-column>
          <el-table-column
            prop="cate_alias"
            label="分类别名"
            >
          </el-table-column>
          <el-table-column
            label="操作">
            <!-- 通过(默认)作用域插槽的方式来获取点击的对象 从而拿到他的id -->
            <template #default="{row}">
              <el-button type="primary" @click="getArtCateById(row.id)">修改</el-button>
              <el-button type="danger" @click="delArtCate(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog @close="resetForm" title="添加文章分类" :visible.sync="addVisible" width="40%">
      <span>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="ruleForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="ruleForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addArtCate">添 加</el-button>
      </span>
    </el-dialog>
    <!-- 修改数据 这个地方关闭对话框不需要清除表单 所以点击要去掉close事件 -->
    <el-dialog  title="修改文章分类" :visible.sync="uDialogVisible" width="40%">
      <span>
        <el-form :model="uRuleForm" :rules="rules" ref="uRuleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="uRuleForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="uRuleForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="uDialogVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="updateArtCate">修 改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data(){
    return{
      addVisible:false,
      uDialogVisible:false,
      list:[],
      ruleForm:{
        cate_name:'',
        cate_alias:''
      },
      uRuleForm:{
        cate_name:'',
        cate_alias:''
      },
      // 校验规则
      rules:{
         cate_name: [
        { required: true, message: '请输入分类名称', trigger: 'blur' },
        { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
      ],
      cate_alias: [
        { required: true, message: '请输入分类别名', trigger: 'blur' },
        { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
      ]
      }
    }
  },
  created(){
    // 调用获取图书分类的方法
    this.getArtCateList()
  },
  methods:{
    // 修改文章分类
    updateArtCate(){
       // 兜底校验
      this.$refs.uRuleForm.validate(async valid=>{
        if(!valid) return
        const {data:res} = await this.$http.put('/my/cate/info',this.uRuleForm)
        if(res.code!==0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新请求数据渲染页面
        this.getArtCateList()
        // 关闭对话框
        this.uDialogVisible = false
      })
    },

    // 获取文章分类的列表
    async getArtCateList(){
      const {data:res} = await this.$http.get('/my/cate/list')
      if(res.code!==0) return
      console.log(res)
      this.list = res.data
    },

    // 关闭对话框时清空表单
    resetForm(){
      this.$refs.ruleForm.resetFields()
    },

    // 添加文章分类
    addArtCate(){
      // 兜底校验
      this.$refs.ruleForm.validate(async valid=>{
        if(!valid) return
        const {data:res} = await this.$http.post('/my/cate/add',this.ruleForm)
        if(res.code!==0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新请求数据渲染页面
        this.getArtCateList()
        // 关闭对话框
        this.addVisible = false
      })
    },

    // 根据id获取文章分类的数据
    async getArtCateById(id){
      const {data:res} = await this.$http.get('/my/cate/info',{params:{id}})
      // console.log(res)
      if(res.code!==0) return
      this.uRuleForm = res.data
      this.uDialogVisible = true
    },
     // 删除选择的文章分类
    delArtCate(id){
        this.$confirm('此操作将永久删除改文章分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data:res} = await this.$http.delete('/my/cate/del',{params:{id}})
          if(res.code!==0) return this.$message.error(res.message)
          this.$message.success(res.message)
          // 重新渲染数据
          this.getArtCateList()
        }).catch(() => {
                  
        });
    },
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
